package com.infoland.afanmall.base.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.model.story.activity.StoryInfoActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WelcomeGifActivity extends AppCompatActivity {
    //    GifImageView welcomeImg;
    @BindView(R.id.welcome_img)
    ImageView welcomeImg;
    Handler handler;
    int time = 5;
    @BindView(R.id.dao_ji_shi)
    TextView daoJiShi;
    boolean isClick = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_gif_activity);
        ButterKnife.bind(this);
        daoJiShi.setText("跳过(" + time + ")S");
        daoJishi();
        Glide.with(this).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_1000" +
                "0&sec=1529576673933&di=499e2d525e1df5de211c21195e708f7d&imgtype=0&src=http%3A%2F%2Fwww.qz123.com%2FUpl" +
                "oads%2FImages%2F20171211%2F6364860820388253169036792.gif").into(welcomeImg);
        daoJiShi.setOnClickListener(v -> {
            go();
        });
    }

    Runnable runnable = null;

    public void daoJishi() {
        handler = new Handler();
        runnable = () -> {
            time -= 1;
            if (time == 1) {
                go();
            } else {
                daoJiShi.setText("跳过(" + time + ")S");
                handler.postDelayed(runnable, 1000);
            }
        };
        handler.postDelayed(runnable, 1000);
    }

    private void go() {
        if (isClick) {
            isClick = false;
            handler.removeCallbacks(runnable);
            daoJiShi.setText("跳过(" + time + ")S");
            ToolsKit.go(MainActivity.class);
            finish();
        }
    }

    @Override
    public void onBackPressed() {
    }
}